// pages/orderCenter/ghgOrder/index.js
const app = getApp();
var topOpacity = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "规划馆预约", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent',
      goUrl:'/pages/orderCenter/index'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    road: '身份类型',
    roadList: [{
      id: 0,
      name: '同行'
    }, {
      id: 1,
      name: '媒体'
    }],
    bmNum: 0, //报名人数
    dateList: [],
    chooseDate: -1, //日期选择
    sjdArr: [{
        id: 0,
        img: 'images/orderCenter/am1.png',
        isBlack: 0
      },{
        id: 1,
        img: 'images/orderCenter/pm1.png',
        isBlack: 0
      }, {
        id: 2,
        img: 'images/orderCenter/pm4.png',
        isBlack: 0
      }
    ],
    chooseTime: -1,
    showMonment: true, //是否显示须知

    openid: '',
    shareTitle: '',
    shareImg: '',
    is_register: 0,
    tipsContent: '',
    list: [],
    pageIndex: 2
  },
  onPageScroll(t) {
    if (t.scrollTop < 40) {
      topOpacity = 0
      if (this.data.topOpacity != 0) {
        this.setData({
          topOpacity: 0
        })
      }
    } else if (t.scrollTop >= 40) {
      if (topOpacity <= 1) {
        topOpacity = t.scrollTop / 100 - .4
      } else {
        topOpacity = 1
      }
      if (topOpacity <= 1) {
        this.setData({
          topOpacity: topOpacity
        })
      }
    }
  },
  //点击我知晓了
  closeMonment() {
    let that = this;
    that.setData({
      showMonment: false
    })
  },
  //选择日期
  chooseDate(e) {

    let that = this;
    let id = e.currentTarget.dataset.id;

    var list = that.data.dateList;
    if (list[id]['status'] == '暂停预约') {
      app.showTip('抱歉,您选择的时间已经暂停预约了哦,请选择其他时间预约');
      return;
    } else if (list[id]['status'] == '预约已满') {
      app.showTip('抱歉,您选择的时间预约人数已满了哦,请选择其他时间预约');
      return;
    }

    //更改选择的时间状态
    that.setData({
      chooseDate: id
    });


    wx.showLoading({
      title: '时间段状态切换中...'
    })
    //后台请求时段是否满了
    wx.request({
      url: that.data.baseUrl + 'checkSjcIsBmLy.php',
      method: 'POST',
      data: {
        bm_time: list[id].ymd,
        page: that.data.pageIndex
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.status == 1) {
          //规划馆
          var sjdArr = res.data.sjdArr;
          for (var i = 0; i < sjdArr.length; i++) {
            if (sjdArr[i].is_black == 1) {
              that.setData({
                ['sjdArr[' + i + '].isBlack']: 1
              })
              if (that.data.chooseTime == i) {
                that.setData({
                  chooseTime: -1
                })
              }
            } else {
              that.setData({
                ['sjdArr[' + i + '].isBlack']: 0
              })
            }
          }
        }
        wx.hideLoading();
      },
      fail: function() {
        wx.hideLoading();
      }
    });

  },
  //时段选择
  chooseTime(e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    //判断是否预约已满了
    if (that.data.sjdArr[id].isBlack == 1) {
      return;
    }
    that.setData({
      chooseTime: id
    })
  },
  add() {
    let that = this;
    let bmNum = that.data.bmNum;
    if(bmNum<5){
      bmNum++;
    }
    that.setData({
      bmNum
    })
  },
  reduce() {
    let that = this;
    let bmNum = that.data.bmNum;
    if (bmNum > 0) {
      bmNum--;
      that.setData({
        bmNum
      })
    }

  },
  //选择了解途径
  roadChange(e) {
    let that = this;
    that.setData({
      road: that.data.roadList[e.detail.value].name,
    })
  },
  //预约
  order() {
    let that = this;

    //判断是否注册过了
    if (that.data.is_register == 0) {
      wx.showModal({
        title: '温馨提示',
        content: that.data.tipsContent,
        complete: (res) => {
          wx.setStorageSync('loginHeaderUrl', '/pages/orderCenter/ghgOrder/index');
          wx.setStorageSync('loginHeaderType', 'redirectTo')
          if (res.confirm) {
            //跳转注册界面
            wx.redirectTo({
              url: '/pages/login/select-login/select-login'
            })
          }
        }
      })
      return;
    }

    let bmNum = that.data.bmNum;
    let chooseDate = that.data.chooseDate;
    let chooseTime = that.data.chooseTime;
    let road = that.data.road;

    if (bmNum == 0) {
      app.showTip("请确定报名人数");
      return;
    }
    if (chooseDate == -1) {
      app.showTip("请选择日期");
      return;
    }
    if (chooseTime == -1) {
      app.showTip("请选择时段");
      return;
    }
    if (road == '身份类型') {
      app.showTip("请选择身份类型");
      return;
    }


    //提交数据
    wx.showLoading({
      title: '数据提交中...'
    });

    wx.request({
      url: that.data.baseUrl + 'addGhgBm.php',
      method: 'POST',
      data: {
        openid: that.data.openid,
        createtime: that.data.dateList[that.data.chooseDate].ymd,
        bmPeople: bmNum,
        yy_time: that.data.chooseTime,
        people_type: road
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        wx.hideLoading();
        app.showTip(res.data.msg)
        if (res.data.status == 1) {
          //后台请求成功
          setTimeout(function() {
            wx.redirectTo({
              url: '/pages/my/orderList'
            })
          }, 1500);
        }
      },
      fail: function(res) {
        wx.hideLoading();
        app.showTip('当前报名人数过多,请稍候重试');
      }
    });

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    //获取缓存中的openid
    var openid = wx.getStorageSync('ly24_openid');

    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function(res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function(res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getGhgOrderPageInfo(res.data.openid);
              },
              fail: function(res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getGhgOrderPageInfo(openid);
    }

    //调用分享
    that.getShareInfo();
  },

  //获取本页数据
  getGhgOrderPageInfo(openid) {
    var that = this;

    wx.showLoading({
      title: '数据加载中...'
    });

    wx.request({
      url: that.data.baseUrl + 'getGhgOrderPageInfo.php',
      method: 'POST',
      data: {
        openid: openid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //后台请求成功
          that.setData({
            is_register: res.data.is_register,
            tipsContent: res.data.tipsContent,
            dateList: res.data.list
          });
        } else {
          //后台请求失败
          app.showTip(res.data.msg);
          return;
        }
      },
      fail: function(res) {
        wx.hideLoading();
      }
    });
  },

  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo.php',
      method: 'POST',
      data: {
        id:37,
        pid:7
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function(res) {}
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/orderCenter/ghgOrder/index',
      imageUrl: that.data.shareImg,
      success: function(res) {

      },
      fail: function(res) {

      }
    }
  }
})